package com.glory.hikvisionmobile.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.adapter.EqpCheckCheckListAdapter;
import com.glory.hikvisionmobile.adapter.EqpCheckInputListAdapter;
import com.glory.hikvisionmobile.base.BaseActivity;
import com.glory.hikvisionmobile.base.Constant;
import com.glory.hikvisionmobile.bean.EqpManger.EqpCheckBean;
import com.glory.hikvisionmobile.bean.EqpManger.EqpCheckInputListBean;
import com.glory.hikvisionmobile.bean.EqpManger.EqpCheckListBean;
import com.glory.hikvisionmobile.bean.EqpManger.EqpCheckSelectorListBean;
import com.glory.hikvisionmobile.bean.EqpManger.EqpKeepBean;
import com.glory.hikvisionmobile.bean.EqpManger.EqpKeepSelectorListBean;
import com.glory.hikvisionmobile.bean.net.BaseResponseBean;
import com.glory.hikvisionmobile.utils.DialogUtils;
import com.glory.hikvisionmobile.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class Eqp_EqpCheckDetailsActivity extends BaseActivity{

    @BindView(R.id.rvKeepCheckList)
    RecyclerView rvKeepCheckList;
    @BindView(R.id.rvKeepInputList)
    RecyclerView rvKeepInputList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvReplay)
    TextView tvReplay;
    @BindView(R.id.llCheckLayout)
    LinearLayout llCheckLayout;
    @BindView(R.id.llInputLayout)
    LinearLayout llInputLayout;
    @BindView(R.id.tvSave)
    TextView tvSave;
    private String scheduleRrn;
    private EqpCheckCheckListAdapter eqpKeepCheckListAdapter;
    private EqpCheckInputListAdapter eqpKeepInputListAdapter;
    private List<EqpCheckSelectorListBean> checkTableLineDatas;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_eqp_check_detials;
    }

    @Override
    protected void initView() {
        Bundle bundle = getIntent().getExtras();
        scheduleRrn = bundle.getString("scheduleRrn");

        rvKeepCheckList.setLayoutManager(new LinearLayoutManager(Eqp_EqpCheckDetailsActivity.this));
        rvKeepCheckList.setItemAnimator(new DefaultItemAnimator());
        rvKeepCheckList.addItemDecoration(new DividerItemDecoration(Eqp_EqpCheckDetailsActivity.this, 1));
        eqpKeepCheckListAdapter = new EqpCheckCheckListAdapter(null);
        eqpKeepCheckListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        eqpKeepCheckListAdapter.isFirstOnly(true);
        rvKeepCheckList.setAdapter(eqpKeepCheckListAdapter);

        rvKeepInputList.setLayoutManager(new LinearLayoutManager(Eqp_EqpCheckDetailsActivity.this));
        rvKeepInputList.setItemAnimator(new DefaultItemAnimator());
        rvKeepInputList.addItemDecoration(new DividerItemDecoration(Eqp_EqpCheckDetailsActivity.this, 1));
        eqpKeepInputListAdapter = new EqpCheckInputListAdapter(null,true);
        eqpKeepInputListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        eqpKeepInputListAdapter.isFirstOnly(true);
        rvKeepInputList.setAdapter(eqpKeepInputListAdapter);
    }

    @Override
    protected void initData() {
        JsonObject map = new JsonObject();
        map.addProperty("actionType", "queryScheduleForm");
        map.addProperty("scheduleRrn", scheduleRrn);
        NetUtils.requestDara("HIK.PdaSpotCheckSchedule", this, map, new FreeUI_DialogEntityCallBack<BaseResponseBean<EqpCheckBean>>(
                new TypeToken<BaseResponseBean<EqpCheckBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<EqpCheckBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    EqpCheckBean eqpCheckBean = response.body().getBody();
                    if (eqpCheckBean.getXPdaPmsSheetCheckTableLineDatas().size() > 0){
                        llCheckLayout.setVisibility(View.VISIBLE);
                        checkTableLineDatas = eqpCheckBean.getXPdaPmsSheetCheckTableLineDatas();
                        eqpKeepCheckListAdapter.setNewData(checkTableLineDatas);
                    }

                    if (eqpCheckBean.getXPdaPmsDatasLineDatas().size() > 0){
                        llInputLayout.setVisibility(View.VISIBLE);
                        eqpKeepInputListAdapter.setNewData(eqpCheckBean.getXPdaPmsDatasLineDatas());
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<EqpCheckBean>> response) {
                super.onError(response);
                loadError(response.getException(), "HIK.PdaSpotCheckSchedule");
            }
        });

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("设备点检详情");
        freeUI_titleView.setRightListener(R.drawable.selector_refresh, v -> {
            eqpKeepCheckListAdapter.getData().clear();
            eqpKeepCheckListAdapter.notifyDataSetChanged();
            eqpKeepInputListAdapter.getData().clear();
            eqpKeepInputListAdapter.notifyDataSetChanged();
            eqpKeepInputListAdapter.clearData();
            initData();
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    @OnClick({R.id.tvReplay,R.id.tvSave})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.tvSave:
                DialogUtils.showLoginDialog(this, Constant.CURRENT_USER, () -> submit("false"),getSupportFragmentManager());
                break;
            case R.id.tvReplay:

                if (!emptyData(eqpKeepInputListAdapter.getData())) {
                    return;
                }

                List<EqpCheckSelectorListBean> data = eqpKeepCheckListAdapter.getData();
                for (EqpCheckSelectorListBean bean : data){
                    if (FreeApi_StringUtils.isEmpty(bean.getReserved1())){
                        showToast("点检状态项不能为空",false);
                        return;
                    }
                }

                if (eqpKeepInputListAdapter.getOutData().size() > 0) {
                    FreeApi_DialogUtils.getConfirmAndCancelDialog(this, "确定", "取消", "当前数值已超限，是否继续", ()
                            -> DialogUtils.showLoginDialog(Eqp_EqpCheckDetailsActivity.this, Constant.CURRENT_USER, ()
                            -> submit("true"),getSupportFragmentManager()), () -> {
                    }).show(getSupportFragmentManager(),"SUBMITDIALOG");
                }else {
                    DialogUtils.showLoginDialog(this, Constant.CURRENT_USER, () -> submit("true"),getSupportFragmentManager());
                }

                break;
        }
    }

    private boolean emptyData(List<EqpCheckInputListBean> data) {

        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).getIsMandatory().equals("Y")){
                if (FreeApi_StringUtils.isEmpty(data.get(i).getDataString())){
                    showToast(data.get(i).getDataName() + "项为必填项，不能为空",false);
                    return false;
                }
            }
        }

        return true;
    }

    private void submit(String isFinished) {
        JsonObject map = new JsonObject();
        JsonObject checkJson = new JsonObject();
        checkJson.add("XPdaPmsSheetCheckTableLineData",new JsonParser().parse(new Gson().toJson(eqpKeepCheckListAdapter.getData())).getAsJsonArray());
        JsonObject inputJson = new JsonObject();
        inputJson.add("XPdaPmsDatasLineData",new JsonParser().parse(new Gson().toJson(eqpKeepInputListAdapter.getData())).getAsJsonArray());
        map.addProperty("actionType", "saveScheduleForm");
        map.addProperty("isFinished", isFinished);
        map.add("XPdaPmsDatasLineDatas",inputJson);
        map.add("XPdaPmsSheetCheckTableLineDatas",checkJson);
        map.addProperty("scheduleRrn",scheduleRrn);
        NetUtils.requestDara("HIK.PdaSpotCheckSchedule", this, map, new FreeUI_DialogEntityCallBack<BaseResponseBean<EqpKeepBean>>(
                new TypeToken<BaseResponseBean<EqpKeepBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<EqpKeepBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    showConfirmDialog(Eqp_EqpCheckDetailsActivity.this,isFinished.equals("true") ? "保存成功" : "临时保存成功");

                }
            }

            @Override
            public void onError(Response<BaseResponseBean<EqpKeepBean>> response) {
                super.onError(response);
                loadError(response.getException(), "HIK.PdaSpotCheckSchedule");
            }
        });
    }

}
